package com.shop.controller;


import com.shop.entity.User;
import com.shop.service.CartService;
import com.shop.service.OrderService;
import com.shop.service.UserService;
import com.shop.service.util.JwtUtil;
import com.shop.service.vo.CartVo;
import com.shop.service.vo.OrderItemVo;
import com.shop.util.CookieUtils;
import com.shop.util.UserTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author D
 * @since 2023-03-11 03:02:04
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private UserService userService;

    @RequestMapping("/preview")
    public List<CartVo> preview(@RequestParam(value = "productIds[]")Integer[] productIds, HttpServletRequest request){
        User user = UserTokenUtil.getUser(request,userService);
        return cartService.searchCartByIds(user.getId(),productIds);
    }
    @RequestMapping("/addOrder")
    public void addOrder(@RequestParam(value = "productIds[]")Integer[] productIds, HttpServletRequest request){
        User user = UserTokenUtil.getUser(request,userService);
        orderService.addOrder(user.getId(),productIds);
    }
    @RequestMapping("/listOrders")
    public OrderItemVo listOrders(HttpServletRequest request){
        User user = UserTokenUtil.getUser(request,userService);
        return orderService.listOrders(user.getId());
    }
}

